package com.byx.chatclient.adapter;

import android.view.View;

import com.byx.chatclient.R;
import com.byx.chatclient.model.ChatPo;
import com.byx.chatclient.widget.QQBezierView;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.module.DraggableModule;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.facebook.drawee.view.SimpleDraweeView;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;

/**
 * ConversationAdapter
 *
 * @author Superb
 * @date 2021/4/22 11:20
 */
public class ConversationAdapter extends BaseQuickAdapter<ChatPo, BaseViewHolder> implements DraggableModule {
    private static final int LAYOUT_RES_ID = R.layout.fragment_home_item_view;

    public ConversationAdapter(@Nullable List<ChatPo> data) {
        super(LAYOUT_RES_ID, data);
        init();
    }

    public ConversationAdapter() {
        super(LAYOUT_RES_ID);
        init();
    }

    @Override
    public long getItemId(int position) {
        return Long.parseLong(getItem(position).getNickName());
    }

    @Override
    protected void convert(@NotNull BaseViewHolder baseViewHolder, ChatPo chatPo) {
        ((SimpleDraweeView) baseViewHolder.getView(R.id.avatar_small)).setImageURI(chatPo.getAvatar());
        baseViewHolder.setText(R.id.nick_name, chatPo.getNickName());
        baseViewHolder.setText(R.id.simple_message, chatPo.getSimpleMessage());
        if (0 == chatPo.getUnreadCount()){
            baseViewHolder.getView(R.id.message_count).setVisibility(View.INVISIBLE);
        } else {
            baseViewHolder.setText(R.id.message_count,String.valueOf(chatPo.getUnreadCount()));
        }
        baseViewHolder.setText(R.id.send_time, chatPo.getSendTime());
        ((QQBezierView) baseViewHolder.getView(R.id.message_count)).setOnDragListener(new QQBezierView.onDragStatusListener() {
            @Override
            public void onDrag() {

            }

            @Override
            public void onMove() {

            }

            @Override
            public void onRestore() {

            }

            @Override
            public void onDismiss() {
                chatPo.resetMessageCount();
            }
        });
    }

    private void init(){

    }

    /**
     * 重写setData，使messageCount自增，并减少不必要的UI刷新
     * @param index
     * @param data
     */
    @Override
    public void setData(int index, ChatPo data) {
        ChatPo cg = this.getData().get(index);
        cg.increaseMessageCount();
        cg.setSimpleMessage(data.getSimpleMessage());
        cg.setSendTime(data.getSendTime());

        super.setData(index, cg);
    }

    /**
     * 判断是否含有指定发送者的消息，存在则返回索引，不存在则返回-1
     * @param senderId
     * @return position of data
     */
    public int hasData(String senderId){
        List<ChatPo> list = getData();
        for (int i = 0;i<list.size();i++){
            if (list.get(i).getSenderId().equals(senderId)){
                return i;
            }
        }
        return -1;
    }
}
